package com.javaPro.myProject.modules.user.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.common.util.uploadUtil;
import com.javaPro.myProject.modules.user.model.User;
import com.javaPro.myProject.modules.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    private String prefix = "system/user";

    @Autowired
    private UserService userService;
    @Resource
    private uploadUtil uploadUtil;
    /**
     * 查询【用户】列表
     */

    @GetMapping("/list")
    @ResponseBody
    public ListByPage list(User User)
    {
        startPage();
        List<User> list = userService.selectUserList(User);
        return getList("",list);
    }

    /**
     * 查询【用户】信息
     */

    @GetMapping("/detail")
    @ResponseBody
    public AjaxResult detail(User User)
    {

        User user = userService.selectUserById(User);
        return AjaxResult.success(user);
    }



    /**
     * 新增保存【用户】
     */

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(User User)
    {
        return toAjax(userService.insertUser(User));
    }



    /**
     * 修改保存【用户】
     */

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MultipartFile file, User User)
    {
        if(file != null){
        String upload = uploadUtil.upload(file);
        User.setImg(upload);
    }
        int i = userService.updateUser(User);


        return toAjax(i);
    }
    /**
     * 修改保存【用户】
     */

    @PostMapping("/updateUserAcc")
    @ResponseBody
    public AjaxResult updateUserAcc( User User){

        int i = userService.updateUserAcc(User);


        return toAjax(i);
    }


}
